package org.shilong.UI.party;

import org.shilong.MContext.Party.PartyContext;
import org.shilong.UI.R;
import org.shilong.Utility.ProgressDialogUtility;
import org.shilong.Utility.SharedPreferenceUtility;
import org.shilong.Utility.ToastUtility;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

/**
 * 创建新聚会的屏幕
 * 
 * @author cesc
 * 
 */
public class AddNewPartyFrame extends Activity implements OnClickListener {

	private Button party_to_create_button;

	private EditText party_input_title;

	private DatePicker party_input_starttime, party_input_endtime;

	private String[] arr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_add_new_party_comment);

		arr = SharedPreferenceUtility.getPersonInfoFromSharedPreference(this);

		initViews();
	}

	private void initViews() {

		party_to_create_button = (Button) this
				.findViewById(R.id.party_to_create_button);

		party_input_title = (EditText) this
				.findViewById(R.id.party_input_title);
		/*party_input_content = (EditText) this
				.findViewById(R.id.party_input_content);*/

		party_input_starttime = (DatePicker) this
				.findViewById(R.id.party_input_starttime);
		party_input_endtime = (DatePicker) this
				.findViewById(R.id.party_input_endtime);

		party_to_create_button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.party_to_create_button:

			addPartyLogic();

			break;

		default:
			break;
		}

	}

	private void addPartyLogic() {

		String title = party_input_title.getText().toString();
		//String content = party_input_content.getText().toString();

		String start_time = party_input_starttime.getYear() + "-"
				+ ( party_input_starttime.getMonth() + 1)+ "-"
				+ party_input_starttime.getDayOfMonth();
		String end_time = party_input_endtime.getYear() + "-"
				+ ( party_input_endtime.getMonth() + 1) + "-"
				+ party_input_endtime.getDayOfMonth();

		System.out.println("新聚会的开始时间是:" + start_time + "\n" + "结束时间是:"
				+ end_time);

		AddNewPartyTask task = new AddNewPartyTask();

		if (TextUtils.isEmpty(title)) {

			ToastUtility.showToast(AddNewPartyFrame.this, "输入标题");

		} else if (TextUtils.isEmpty(start_time)) {

			ToastUtility.showToast(AddNewPartyFrame.this, "输入开始时间");

		} else if (TextUtils.isEmpty(end_time)) {

			ToastUtility.showToast(AddNewPartyFrame.this, "输入结束时间");

		} else {

			// 用户名、标题、类型、开始时间、结束时间、描述
			task.execute(arr[2], title, "fq", start_time, end_time, "");
		}

	}

	/**
	 * 创建聚会的task
	 * 
	 * @author Administrator
	 * 
	 */
	private class AddNewPartyTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {

			String info = "error";
			info = (String) PartyContext
					.getInstance(PartyContext.ADD_NEW_PARTY).getRlt(params);

			return info;
		}

		@Override
		protected void onPostExecute(String result) {

			ProgressDialogUtility.cancelDialog();

			if (result.equals("add_new_party_succesed")) {

				ToastUtility.showToast(AddNewPartyFrame.this, "创建成功");

				AddNewPartyFrame.this.finish();

				Intent mIntent = new Intent(PartyCenterFrame.ACTION_NAME);
				AddNewPartyFrame.this.sendBroadcast(mIntent);

			} else if (result.equals("add_new_party_failed")) {
				ToastUtility.showToast(AddNewPartyFrame.this, "创建失败");

			}
		}

		@Override
		protected void onPreExecute() {

			ProgressDialogUtility.showDialog(AddNewPartyFrame.this, "创建中..");
		}

	}
}
